package com.example.demo.controller;

import com.example.demo.entity.Organ;
import com.example.demo.enums.ResultEnum;
import com.example.demo.service.OrganService;
import com.example.demo.vo.ResultVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/organ")
public class OrganController {
    @Autowired
    private OrganService organService;

    @RequestMapping("/findAllOrgan")
    public Object findAllOrgan(String page,String rows,String mname,String sort,String order){
        Page<Object> pageHelper = PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(rows));
        List<Organ> lists = organService.findAllOrgan(mname,sort,order);//自动改变为分页查询
        long total = pageHelper.getTotal();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("rows",lists);
        map.put("total",total);
        return map;
    }
    @RequestMapping("getByMid")
    public Object getByMid(String mid){
        Organ o = organService.getByMid(Integer.valueOf(mid));
        return o;
    }

    @RequestMapping("/modifyOrgan")
    public ResultVO modifyOrgan(Organ o){
        organService.modifyOrgan(o);
        return new ResultVO(ResultEnum.SUCCESS,"修改成功");
    }
    @RequestMapping("/removeBatch")
    public ResultVO removeBatch(String[] mids){
        organService.removeBatch(mids);
        return new ResultVO(ResultEnum.SUCCESS,"批量删除成功");
    }

    @RequestMapping("/delByMid")
    public ResultVO delByMid(String mid){
        organService.delByMid(mid);
        return new ResultVO(ResultEnum.SUCCESS,"删除成功");
    }
    @RequestMapping("/addOrgan")
    public ResultVO addOrgan(Organ o){
        organService.addOrgan(o);
        return new ResultVO(ResultEnum.SUCCESS,"添加成功");
    }
}
